home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / unix / uucp106d / part12 < prev   
Encoding:
Internet Message Format  |  1990-06-28  |  43.9 KB

  1. Path: xanth!cs.odu.edu!Amiga-Request
  2. From: Amiga-Request@cs.odu.edu (Amiga Sources/Binaries Moderator)
  3. Newsgroups: comp.sources.amiga
  4. Subject: v90i190: UUCP 1.06D - UNIX compatible uucp/news/mail system, Part12/12
  5. Message-ID: <12981@xanth.cs.odu.edu>
  6. Date: 28 Jun 90 12:23:51 GMT
  7. Sender: news@cs.odu.edu
  8. Reply-To: Matt Dillon <@uunet.uu.net:overload!dillon>
  9. Lines: 1624
  10. Approved: tadguy@cs.odu.edu (Tad Guy)
  11. X-Mail-Submissions-To: Amiga@cs.odu.edu
  12. X-Post-Discussions-To: comp.sys.amiga
  13.  
  14. Submitted-by: Matt Dillon <@uunet.uu.net:overload!dillon>
  15. Posting-number: Volume 90, Issue 190
  16. Archive-name: unix/uucp-1.06d/part12
  17.  
  18. #!/bin/sh
  19. # This is a shell archive.  Remove anything before this line, then unpack
  20. # it by saving it into a file and typing "sh file".  To overwrite existing
  21. # files, type "sh file -c".  You can also feed this as standard input via
  22. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  23. # will see the following message at the end:
  24. #        "End of archive 12 (of 12)."
  25. # Contents:  uucp2/src/unix/compress.c
  26. # Wrapped by tadguy@xanth on Thu Jun 28 08:21:39 1990
  27. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  28. if test -f 'uucp2/src/unix/compress.c' -a "${1}" != "-c" ; then 
  29.   echo shar: Will not clobber existing file \"'uucp2/src/unix/compress.c'\"
  30. else
  31. echo shar: Extracting \"'uucp2/src/unix/compress.c'\" \(41167 characters\)
  32. sed "s/^X//" >'uucp2/src/unix/compress.c' <<'END_OF_FILE'
  33. X
  34. X/*
  35. X * Compress - data compression program
  36. X *
  37. X *  $Header: Beta:src/uucp/src/compress/RCS/compress.c,v 1.1 90/02/02 11:47:55 dillon Exp Locker: dillon $
  38. X *
  39. X */
  40. X
  41. X#include <stdio.h>
  42. X#include "version.h"
  43. X
  44. XIDENT(".00");
  45. X
  46. X/*
  47. X * machine variants which require cc -Dmachine:  pdp11, z8000, pcxt
  48. X */
  49. X
  50. Xvoid Usage();
  51. Xvoid output();
  52. Xvoid writeerr();
  53. Xvoid copystat();
  54. Xvoid onintr();
  55. Xvoid cl_block();
  56. Xvoid cl_hash();
  57. Xvoid prratio();
  58. Xvoid version();
  59. Xvoid oops();
  60. Xchar *rindex();
  61. X
  62. X
  63. X/*
  64. X * Set USERMEM to the maximum amount of physical user memory available
  65. X * in bytes.  USERMEM is used to determine the maximum BITS that can be used
  66. X * for compression.
  67. X *
  68. X * SACREDMEM is the amount of physical memory saved for others; compress
  69. X * will hog the rest.
  70. X */
  71. X
  72. X#ifndef SACREDMEM
  73. X#define SACREDMEM    0
  74. X#endif
  75. X
  76. X#ifndef USERMEM
  77. X/*# ifdef AMIGA
  78. X#   define USERMEM    200000
  79. X# else*/
  80. X#   define USERMEM    450000    /* default user memory */
  81. X/* # endif*/
  82. X#endif
  83. X
  84. X#ifdef interdata        /* (Perkin-Elmer) */
  85. X#define SIGNED_COMPARE_SLOW    /* signed compare is slower than unsigned */
  86. X#endif
  87. X
  88. X#ifdef pdp11
  89. X# define BITS    12    /* max bits/code for 16-bit machine */
  90. X# define NO_UCHAR    /* also if "unsigned char" functions as signed char */
  91. X# undef USERMEM
  92. X#endif /* pdp11 */    /* don't forget to compile with -i */
  93. X
  94. X#ifdef z8000
  95. X# define BITS    12
  96. X# undef vax        /* weird preprocessor */
  97. X# undef USERMEM
  98. X#endif /* z8000 */
  99. X
  100. X#ifdef pcxt
  101. X# define BITS    12
  102. X# undef USERMEM
  103. X#endif /* pcxt */
  104. X
  105. X#ifdef USERMEM
  106. X# if USERMEM >= (433484+SACREDMEM)
  107. X#  define PBITS 16
  108. X# else
  109. X#  if USERMEM >= (229600+SACREDMEM)
  110. X#   define PBITS    15
  111. X#  else
  112. X#   if USERMEM >= (127536+SACREDMEM)
  113. X#    define PBITS    14
  114. X#   else
  115. X#    if USERMEM >= (73464+SACREDMEM)
  116. X#     define PBITS    13
  117. X#    else
  118. X#     define PBITS    12
  119. X#    endif
  120. X#   endif
  121. X#  endif
  122. X# endif
  123. X# undef USERMEM
  124. X#endif /* USERMEM */
  125. X
  126. X#ifdef PBITS        /* Preferred BITS for this memory size */
  127. X# ifndef BITS
  128. X#  define BITS PBITS
  129. X# endif BITS
  130. X#endif /* PBITS */
  131. X
  132. X#if BITS == 16
  133. X# define HSIZE    69001        /* 95% occupancy */
  134. X#endif
  135. X#if BITS == 15
  136. X# define HSIZE    35023        /* 94% occupancy */
  137. X#endif
  138. X#if BITS == 14
  139. X# define HSIZE    18013        /* 91% occupancy */
  140. X#endif
  141. X#if BITS == 13
  142. X# define HSIZE    9001        /* 91% occupancy */
  143. X#endif
  144. X#if BITS <= 12
  145. X# define HSIZE    5003        /* 80% occupancy */
  146. X#endif
  147. X
  148. X#ifdef M_XENIX            /* Stupid compiler can't handle arrays with */
  149. X# if BITS == 16         /* more than 65535 bytes - so we fake it */
  150. X#  define XENIX_16
  151. X# else
  152. X#  if BITS > 13         /* Code only handles BITS = 12, 13, or 16 */
  153. X#   define BITS 13
  154. X#  endif
  155. X# endif
  156. X#endif
  157. X
  158. X/*
  159. X * a code_int must be able to hold 2**BITS values of type int, and also -1
  160. X */
  161. X#if BITS > 15
  162. Xtypedef long int    code_int;
  163. X#else
  164. Xtypedef int        code_int;
  165. X#endif
  166. X
  167. X#ifdef SIGNED_COMPARE_SLOW
  168. Xtypedef unsigned long int count_int;
  169. Xtypedef unsigned short int count_short;
  170. X#else
  171. Xtypedef long int      count_int;
  172. X#endif
  173. X
  174. X#ifdef NO_UCHAR
  175. X typedef char    char_type;
  176. X#else
  177. X typedef    unsigned char    char_type;
  178. X#endif /* UCHAR */
  179. Xchar_type magic_header[] = { "\037\235" };      /* 1F 9D */
  180. X
  181. X/* Defines for third byte of header */
  182. X#define BIT_MASK    0x1f
  183. X#define BLOCK_MASK    0x80
  184. X/* Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
  185. X   a fourth header byte (for expansion).
  186. X*/
  187. X#define INIT_BITS 9            /* initial number of bits/code */
  188. X
  189. X/*
  190. X * compress.c - File compression ala IEEE Computer, June 1984.
  191. X *
  192. X * Authors:    Spencer W. Thomas    (decvax!harpo!utah-cs!utah-gr!thomas)
  193. X *        Jim McKie        (decvax!mcvax!jim)
  194. X *        Steve Davies        (decvax!vax135!petsd!peora!srd)
  195. X *        Ken Turkowski        (decvax!decwrl!turtlevax!ken)
  196. X *        James A. Woods        (decvax!ihnp4!ames!jaw)
  197. X *        Joe Orost        (decvax!vax135!petsd!joe)
  198. X *
  199. X * $Header: Beta:src/uucp/src/compress/RCS/compress.c,v 1.1 90/02/02 11:47:55 dillon Exp Locker: dillon $
  200. X * $Log:    compress.c,v $
  201. X * Revision 1.1  90/02/02  11:47:55  dillon
  202. X * Initial revision
  203. X *
  204. X * Revision 4.0  85/07/30  12:50:00  joe
  205. X * Removed ferror() calls in output routine on every output except first.
  206. X * Prepared for release to the world.
  207. X *
  208. X * Revision 3.6  85/07/04  01:22:21  joe
  209. X * Remove much wasted storage by overlaying hash table with the tables
  210. X * used by decompress: tab_suffix[1<<BITS], stack[8000].  Updated USERMEM
  211. X * computations.  Fixed dump_tab() DEBUG routine.
  212. X *
  213. X * Revision 3.5  85/06/30  20:47:21  jaw
  214. X * Change hash function to use exclusive-or.  Rip out hash cache.  These
  215. X * speedups render the megamemory version defunct, for now.  Make decoder
  216. X * stack global.  Parts of the RCS trunks 2.7, 2.6, and 2.1 no longer apply.
  217. X *
  218. X * Revision 3.4  85/06/27  12:00:00  ken
  219. X * Get rid of all floating-point calculations by doing all compression ratio
  220. X * calculations in fixed point.
  221. X *
  222. X * Revision 3.3  85/06/24  21:53:24  joe
  223. X * Incorporate portability suggestion for M_XENIX.  Got rid of text on #else
  224. X * and #endif lines.  Cleaned up #ifdefs for vax and interdata.
  225. X *
  226. X * Revision 3.2  85/06/06  21:53:24  jaw
  227. X * Incorporate portability suggestions for Z8000, IBM PC/XT from mailing list.
  228. X * Default to "quiet" output (no compression statistics).
  229. X *
  230. X * Revision 3.1  85/05/12  18:56:13  jaw
  231. X * Integrate decompress() stack speedups (from early pointer mods by McKie).
  232. X * Repair multi-file USERMEM gaffe.  Unify 'force' flags to mimic semantics
  233. X * of SVR2 'pack'.  Streamline block-compress table clear logic.  Increase
  234. X * output byte count by magic number size.
  235. X *
  236. X * Revision 3.0   84/11/27  11:50:00  petsd!joe
  237. X * Set HSIZE depending on BITS.  Set BITS depending on USERMEM.  Unrolled
  238. X * loops in clear routines.  Added "-C" flag for 2.0 compatibility.  Used
  239. X * unsigned compares on Perkin-Elmer.  Fixed foreground check.
  240. X *
  241. X * Revision 2.7   84/11/16  19:35:39  ames!jaw
  242. X * Cache common hash codes based on input statistics; this improves
  243. X * performance for low-density raster images.  Pass on #ifdef bundle
  244. X * from Turkowski.
  245. X *
  246. X * Revision 2.6   84/11/05  19:18:21  ames!jaw
  247. X * Vary size of hash tables to reduce time for small files.
  248. X * Tune PDP-11 hash function.
  249. X *
  250. X * Revision 2.5   84/10/30  20:15:14  ames!jaw
  251. X * Junk chaining; replace with the simpler (and, on the VAX, faster)
  252. X * double hashing, discussed within.  Make block compression standard.
  253. X *
  254. X * Revision 2.4   84/10/16  11:11:11  ames!jaw
  255. X * Introduce adaptive reset for block compression, to boost the rate
  256. X * another several percent.  (See mailing list notes.)
  257. X *
  258. X * Revision 2.3   84/09/22  22:00:00  petsd!joe
  259. X * Implemented "-B" block compress.  Implemented REVERSE sorting of tab_next.
  260. X * Bug fix for last bits.  Changed fwrite to putchar loop everywhere.
  261. X *
  262. X * Revision 2.2   84/09/18  14:12:21  ames!jaw
  263. X * Fold in news changes, small machine typedef from thomas,
  264. X * #ifdef interdata from joe.
  265. X *
  266. X * Revision 2.1   84/09/10  12:34:56  ames!jaw
  267. X * Configured fast table lookup for 32-bit machines.
  268. X * This cuts user time in half for b <= FBITS, and is useful for news batching
  269. X * from VAX to PDP sites.  Also sped up decompress() [fwrite->putc] and
  270. X * added signal catcher [plus beef in writeerr()] to delete effluvia.
  271. X *
  272. X * Revision 2.0   84/08/28  22:00:00  petsd!joe
  273. X * Add check for foreground before prompting user.  Insert maxbits into
  274. X * compressed file.  Force file being uncompressed to end with ".Z".
  275. X * Added "-c" flag and "zcat".  Prepared for release.
  276. X *
  277. X * Revision 1.10  84/08/24  18:28:00  turtlevax!ken
  278. X * Will only compress regular files (no directories), added a magic number
  279. X * header (plus an undocumented -n flag to handle old files without headers),
  280. X * added -f flag to force overwriting of possibly existing destination file,
  281. X * otherwise the user is prompted for a response.  Will tack on a .Z to a
  282. X * filename if it doesn't have one when decompressing.  Will only replace
  283. X * file if it was compressed.
  284. X *
  285. X * Revision 1.9  84/08/16  17:28:00  turtlevax!ken
  286. X * Removed scanargs(), getopt(), added .Z extension and unlimited number of
  287. X * filenames to compress.  Flags may be clustered (-Ddvb12) or separated
  288. X * (-D -d -v -b 12), or combination thereof.  Modes and other status is
  289. X * copied with copystat().  -O bug for 4.2 seems to have disappeared with
  290. X * 1.8.
  291. X *
  292. X * Revision 1.8  84/08/09  23:15:00  joe
  293. X * Made it compatible with vax version, installed jim's fixes/enhancements
  294. X *
  295. X * Revision 1.6  84/08/01  22:08:00  joe
  296. X * Sped up algorithm significantly by sorting the compress chain.
  297. X *
  298. X * Revision 1.5  84/07/13  13:11:00  srd
  299. X * Added C version of vax asm routines.  Changed structure to arrays to
  300. X * save much memory.  Do unsigned compares where possible (faster on
  301. X * Perkin-Elmer)
  302. X *
  303. X * Revision 1.4  84/07/05  03:11:11  thomas
  304. X * Clean up the code a little and lint it.  (Lint complains about all
  305. X * the regs used in the asm, but I'm not going to "fix" this.)
  306. X *
  307. X * Revision 1.3  84/07/05  02:06:54  thomas
  308. X * Minor fixes.
  309. X *
  310. X * Revision 1.2  84/07/05  00:27:27  thomas
  311. X * Add variable bit length output.
  312. X *
  313. X */
  314. X
  315. Xstatic char rcs_ident[] = "$Header: Beta:src/uucp/src/compress/RCS/compress.c,v 1.1 90/02/02 11:47:55 dillon Exp Locker: dillon $";
  316. X
  317. X#include <stdio.h>
  318. X#include <ctype.h>
  319. X#ifdef unix
  320. X#include <signal.h>
  321. X#include <sys/types.h>
  322. X#include <sys/stat.h>
  323. X#endif
  324. X
  325. X#ifndef min
  326. X#define min(a,b)        ((a>b) ? b : a)
  327. X#endif
  328. X
  329. X#define ARGVAL() (*++(*argv) || (--argc && *++argv))
  330. X
  331. Xint n_bits;                /* number of bits/code */
  332. Xint maxbits = BITS;            /* user settable max # bits/code */
  333. Xcode_int maxcode;            /* maximum code, given n_bits */
  334. Xcode_int maxmaxcode = 1 << BITS;    /* should NEVER generate this code */
  335. X#ifdef COMPATIBLE        /* But wrong! */
  336. X# define MAXCODE(n_bits)        (1 << (n_bits) - 1)
  337. X#else
  338. X# define MAXCODE(n_bits)        ((1 << (n_bits)) - 1)
  339. X#endif /* COMPATIBLE */
  340. X
  341. X#ifdef XENIX_16
  342. Xcount_int htab0[8192];
  343. Xcount_int htab1[8192];
  344. Xcount_int htab2[8192];
  345. Xcount_int htab3[8192];
  346. Xcount_int htab4[8192];
  347. Xcount_int htab5[8192];
  348. Xcount_int htab6[8192];
  349. Xcount_int htab7[8192];
  350. Xcount_int htab8[HSIZE-65536];
  351. Xcount_int * htab[9] = {
  352. X    htab0, htab1, htab2, htab3, htab4, htab5, htab6, htab7, htab8 };
  353. X
  354. X#define htabof(i)       (htab[(i) >> 13][(i) & 0x1fff])
  355. Xunsigned short code0tab[16384];
  356. Xunsigned short code1tab[16384];
  357. Xunsigned short code2tab[16384];
  358. Xunsigned short code3tab[16384];
  359. Xunsigned short code4tab[16384];
  360. Xunsigned short * codetab[5] = {
  361. X    code0tab, code1tab, code2tab, code3tab, code4tab };
  362. X
  363. X#define codetabof(i)    (codetab[(i) >> 14][(i) & 0x3fff])
  364. X
  365. X#else    /* Normal machine */
  366. Xcount_int htab [HSIZE];
  367. Xunsigned short codetab [HSIZE];
  368. X#define htabof(i)       htab[i]
  369. X#define codetabof(i)    codetab[i]
  370. X#endif    /* XENIX_16 */
  371. Xcode_int hsize = HSIZE;         /* for dynamic table sizing */
  372. Xcount_int fsize;
  373. X
  374. X/*
  375. X * To save much memory, we overlay the table used by compress() with those
  376. X * used by decompress().  The tab_prefix table is the same size and type
  377. X * as the codetab.  The tab_suffix table needs 2**BITS characters.  We
  378. X * get this from the beginning of htab.  The output stack uses the rest
  379. X * of htab, and contains characters.  There is plenty of room for any
  380. X * possible stack (stack used to be 8000 characters).
  381. X */
  382. X
  383. X#define tab_prefixof(i) codetabof(i)
  384. X#ifdef XENIX_16
  385. X# define tab_suffixof(i)        ((char_type *)htab[(i)>>15])[(i) & 0x7fff]
  386. X# define de_stack        ((char_type *)(htab2))
  387. X#else    /* Normal machine */
  388. X# define tab_suffixof(i)        ((char_type *)(htab))[i]
  389. X# define de_stack        ((char_type *)&tab_suffixof(1<<BITS))
  390. X#endif    /* XENIX_16 */
  391. X
  392. Xcode_int free_ent = 0;            /* first unused entry */
  393. Xint exit_stat = 0;
  394. X
  395. Xcode_int getcode();
  396. X
  397. Xvoid
  398. XUsage() {
  399. X#ifdef DEBUG
  400. Xfprintf(stderr,"Usage: compress [-dDVfc] [-b maxbits] [file ...]\n");
  401. X}
  402. Xint debug = 0;
  403. X#else
  404. Xfprintf(stderr,"Usage: compress [-dfvcV] [-b maxbits] [file ...]\n");
  405. X}
  406. X#endif /* DEBUG */
  407. Xint nomagic = 0;    /* Use a 3-byte magic number header, unless old file */
  408. Xint zcat_flg = 0;    /* Write output on stdout, suppress messages */
  409. Xint quiet = 1;        /* don't tell me about compression */
  410. X
  411. X/*
  412. X * block compression parameters -- after all codes are used up,
  413. X * and compression rate changes, start over.
  414. X */
  415. Xint block_compress = BLOCK_MASK;
  416. Xint clear_flg = 0;
  417. Xlong int ratio = 0;
  418. X#define CHECK_GAP 10000 /* ratio check interval */
  419. Xcount_int checkpoint = CHECK_GAP;
  420. X/*
  421. X * the next two codes should not be changed lightly, as they must not
  422. X * lie within the contiguous general code space.
  423. X */
  424. X#define FIRST    257    /* first free entry */
  425. X#define CLEAR    256    /* table clear output code */
  426. X
  427. Xint force = 0;
  428. Xchar ofname [100];
  429. X#ifdef DEBUG
  430. Xint verbose = 0;
  431. X#endif /* DEBUG */
  432. Xint (*bgnd_flag)();
  433. X
  434. Xint do_decomp = 0;
  435. X
  436. Xstatic void decompress ();
  437. Xstatic void compress ();
  438. X
  439. X/*****************************************************************
  440. X * TAG( main )
  441. X *
  442. X * Algorithm from "A Technique for High Performance Data Compression",
  443. X * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
  444. X *
  445. X * Usage: compress [-dfvc] [-b bits] [file ...]
  446. X * Inputs:
  447. X *    -d:        If given, decompression is done instead.
  448. X *
  449. X *    -c:        Write output on stdout, don't remove original.
  450. X *
  451. X *    -b:        Parameter limits the max number of bits/code.
  452. X *
  453. X *    -f:        Forces output file to be generated, even if one already
  454. X *            exists, and even if no space is saved by compressing.
  455. X *            If -f is not used, the user will be prompted if stdin is
  456. X *            a tty, otherwise, the output file will not be overwritten.
  457. X *
  458. X *    -v:        Write compression statistics
  459. X *
  460. X *    file ...:   Files to be compressed.  If none specified, stdin
  461. X *            is used.
  462. X * Outputs:
  463. X *    file.Z:     Compressed form of file with same mode, owner, and utimes
  464. X *    or stdout   (if stdin used as input)
  465. X *
  466. X * Assumptions:
  467. X *    When filenames are given, replaces with the compressed version
  468. X *    (.Z suffix) only if the file decreases in size.
  469. X * Algorithm:
  470. X *    Modified Lempel-Ziv method (LZW).  Basically finds common
  471. X * substrings and replaces them with a variable size code.  This is
  472. X * deterministic, and can be done on the fly.  Thus, the decompression
  473. X * procedure needs no input table, but tracks the way the table was built.
  474. X */
  475. X
  476. X
  477. Xvoid
  478. Xmain( argc, argv )
  479. Xregister int argc; char **argv;
  480. X{
  481. X    int overwrite = 0;    /* Do not overwrite unless given -f flag */
  482. X    char tempname[100];
  483. X    char **filelist, **fileptr;
  484. X    char *cp;
  485. X
  486. X#ifdef unix
  487. X    if ( (bgnd_flag = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) {
  488. X    signal ( SIGINT, onintr );
  489. X    signal ( SIGSEGV, oops );
  490. X    }
  491. X#endif
  492. X
  493. X#ifdef COMPATIBLE
  494. X    nomagic = 1;    /* Original didn't have a magic number */
  495. X#endif /* COMPATIBLE */
  496. X
  497. X    filelist = fileptr = (char **)(malloc(argc * sizeof(*argv)));
  498. X    *filelist = NULL;
  499. X
  500. X    if((cp = rindex(argv[0], '/')) != 0) {
  501. X    cp++;
  502. X    } else {
  503. X    cp = argv[0];
  504. X    }
  505. X    if(strcmp(cp, "uncompress") == 0) {
  506. X    do_decomp = 1;
  507. X    } else if(strcmp(cp, "zcat") == 0) {
  508. X    do_decomp = 1;
  509. X    zcat_flg = 1;
  510. X    }
  511. X
  512. X#ifdef BSD4_2
  513. X    /* 4.2BSD dependent - take it out if not */
  514. X    setlinebuf( stderr );
  515. X#endif /* BSD4_2 */
  516. X
  517. X    /* Argument Processing
  518. X     * All flags are optional.
  519. X     * -D => debug
  520. X     * -V => print Version; debug verbose
  521. X     * -d => do_decomp
  522. X     * -v => unquiet
  523. X     * -f => force overwrite of output file
  524. X     * -n => no header: useful to uncompress old files
  525. X     * -b maxbits => maxbits.  If -b is specified, then maxbits MUST be
  526. X     *        given also.
  527. X     * -c => cat all output to stdout
  528. X     * -C => generate output compatible with compress 2.0.
  529. X     * if a string is left, must be an input filename.
  530. X     */
  531. X    for (argc--, argv++; argc > 0; argc--, argv++) {
  532. X    if (**argv == '-') {    /* A flag argument */
  533. X        while (*++(*argv)) {        /* Process all flags in this arg */
  534. X        switch (**argv) {
  535. X#ifdef DEBUG
  536. X            case 'D':
  537. X            debug = 1;
  538. X            break;
  539. X            case 'V':
  540. X            verbose = 1;
  541. X            version();
  542. X            break;
  543. X#else
  544. X            case 'V':
  545. X            version();
  546. X            break;
  547. X#endif /* DEBUG */
  548. X            case 'v':
  549. X            quiet = 0;
  550. X            break;
  551. X            case 'd':
  552. X            do_decomp = 1;
  553. X            break;
  554. X            case 'f':
  555. X            case 'F':
  556. X            overwrite = 1;
  557. X            force = 1;
  558. X            break;
  559. X            case 'n':
  560. X            nomagic = 1;
  561. X            break;
  562. X            case 'C':
  563. X            block_compress = 0;
  564. X            break;
  565. X            case 'b':
  566. X            if (!ARGVAL()) {
  567. X                fprintf(stderr, "Missing maxbits\n");
  568. X                Usage();
  569. X                exit(1);
  570. X            }
  571. X            maxbits = atoi(*argv);
  572. X            goto nextarg;
  573. X            case 'c':
  574. X            zcat_flg = 1;
  575. X            break;
  576. X            case 'q':
  577. X            quiet = 1;
  578. X            break;
  579. X            default:
  580. X            fprintf(stderr, "Unknown flag: '%c'; ", **argv);
  581. X            Usage();
  582. X            exit(1);
  583. X        }
  584. X        }
  585. X    }
  586. X    else {        /* Input file name */
  587. X        *fileptr++ = *argv; /* Build input file list */
  588. X        *fileptr = NULL;
  589. X        /* process nextarg; */
  590. X    }
  591. X    nextarg: continue;
  592. X    }
  593. X
  594. X    if(maxbits < INIT_BITS) maxbits = INIT_BITS;
  595. X    if (maxbits > BITS) maxbits = BITS;
  596. X    maxmaxcode = 1 << maxbits;
  597. X
  598. X    if (*filelist != NULL) {
  599. X    for (fileptr = filelist; *fileptr; fileptr++) {
  600. X        exit_stat = 0;
  601. X        if (do_decomp != 0) {                       /* DECOMPRESSION */
  602. X        /* Check for .Z suffix */
  603. X        if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") != 0) {
  604. X            /* No .Z: tack one on */
  605. X            strcpy(tempname, *fileptr);
  606. X            strcat(tempname, ".Z");
  607. X            *fileptr = tempname;
  608. X        }
  609. X        /* Open input file */
  610. X        if ((freopen(*fileptr, "r", stdin)) == NULL) {
  611. X            perror(*fileptr); continue;
  612. X        }
  613. X        /* Check the magic number */
  614. X        if (nomagic == 0) {
  615. X            if ((getchar() != (magic_header[0] & 0xFF))
  616. X             || (getchar() != (magic_header[1] & 0xFF))) {
  617. X            fprintf(stderr, "%s: not in compressed format\n",
  618. X                *fileptr);
  619. X            continue;
  620. X            }
  621. X            maxbits = getchar();        /* set -b from file */
  622. X            block_compress = maxbits & BLOCK_MASK;
  623. X            maxbits &= BIT_MASK;
  624. X            maxmaxcode = 1 << maxbits;
  625. X            if(maxbits > BITS) {
  626. X            fprintf(stderr,
  627. X            "%s: compressed with %d bits, can only handle %d bits\n",
  628. X            *fileptr, maxbits, BITS);
  629. X            continue;
  630. X            }
  631. X        }
  632. X        /* Generate output filename */
  633. X        strcpy(ofname, *fileptr);
  634. X        ofname[strlen(*fileptr) - 2] = '\0';  /* Strip off .Z */
  635. X        } else {                    /* COMPRESSION */
  636. X        if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") == 0) {
  637. X            fprintf(stderr, "%s: already has .Z suffix -- no change\n",
  638. X                *fileptr);
  639. X            continue;
  640. X        }
  641. X        /* Open input file */
  642. X        if ((freopen(*fileptr, "r", stdin)) == NULL) {
  643. X            perror(*fileptr); continue;
  644. X        }
  645. X        fsize = getfilesize (*fileptr);
  646. X        /*
  647. X         * tune hash table size for small files -- ad hoc,
  648. X         * but the sizes match earlier #defines, which
  649. X         * serve as upper bounds on the number of output codes.
  650. X         */
  651. X        hsize = HSIZE;
  652. X        if ( fsize < (1 << 12) )
  653. X            hsize = min ( 5003, HSIZE );
  654. X        else if ( fsize < (1 << 13) )
  655. X            hsize = min ( 9001, HSIZE );
  656. X        else if ( fsize < (1 << 14) )
  657. X            hsize = min ( 18013, HSIZE );
  658. X        else if ( fsize < (1 << 15) )
  659. X            hsize = min ( 35023, HSIZE );
  660. X        else if ( fsize < 47000 )
  661. X            hsize = min ( 50021, HSIZE );
  662. X
  663. X        /* Generate output filename */
  664. X        strcpy(ofname, *fileptr);
  665. X#ifndef BSD4_2        /* Short filenames */
  666. X        if ((cp=rindex(ofname,'/')) != NULL)    cp++;
  667. X        else                    cp = ofname;
  668. X        if (strlen(cp) > 12) {
  669. X            fprintf(stderr,"%s: filename too long to tack on .Z\n",cp);
  670. X            continue;
  671. X        }
  672. X#endif    /* BSD4_2        Long filenames allowed */
  673. X        strcat(ofname, ".Z");
  674. X        }
  675. X        /* Check for overwrite of existing file */
  676. X#ifdef unix
  677. X        if (overwrite == 0 && zcat_flg == 0) {
  678. X        if (stat(ofname, &statbuf) == 0) {
  679. X            char response[2];
  680. X            response[0] = 'n';
  681. X            fprintf(stderr, "%s already exists;", ofname);
  682. X            if (foreground()) {
  683. X            fprintf(stderr, " do you wish to overwrite %s (y or n)? ",
  684. X            ofname);
  685. X            fflush(stderr);
  686. X            read(2, response, 2);
  687. X            while (response[1] != '\n') {
  688. X                if (read(2, response+1, 1) < 0) {   /* Ack! */
  689. X                perror("stderr"); break;
  690. X                }
  691. X            }
  692. X            }
  693. X            if (response[0] != 'y') {
  694. X            fprintf(stderr, "\tnot overwritten\n");
  695. X            continue;
  696. X            }
  697. X        }
  698. X        }
  699. X#endif
  700. X        if(zcat_flg == 0) {         /* Open output file */
  701. X        if (freopen(ofname, "w", stdout) == NULL) {
  702. X            perror(ofname);
  703. X            continue;
  704. X        }
  705. X        if(!quiet)
  706. X            fprintf(stderr, "%s: ", *fileptr);
  707. X        }
  708. X
  709. X        /* Actually do the compression/decompression */
  710. X        if (do_decomp == 0) compress();
  711. X#ifndef DEBUG
  712. X        else            decompress();
  713. X#else
  714. X        else if (debug == 0)        decompress();
  715. X        else            printcodes();
  716. X        if (verbose)                dump_tab();
  717. X#endif /* DEBUG */
  718. X        if(zcat_flg == 0) {
  719. X        copystat(*fileptr, ofname);     /* Copy stats */
  720. X        if((exit_stat == 1) || (!quiet))
  721. X            putc('\n', stderr);
  722. X        }
  723. X    }
  724. X    } else {        /* Standard input */
  725. X    if (do_decomp == 0) {
  726. X        compress();
  727. X#ifdef DEBUG
  728. X        if(verbose)             dump_tab();
  729. X#endif /* DEBUG */
  730. X        if(!quiet)
  731. X            putc('\n', stderr);
  732. X    } else {
  733. X        /* Check the magic number */
  734. X        if (nomagic == 0) {
  735. X        if ((getchar()!=(magic_header[0] & 0xFF))
  736. X         || (getchar()!=(magic_header[1] & 0xFF))) {
  737. X            fprintf(stderr, "stdin: not in compressed format\n");
  738. X            exit(1);
  739. X        }
  740. X        maxbits = getchar();    /* set -b from file */
  741. X        block_compress = maxbits & BLOCK_MASK;
  742. X        maxbits &= BIT_MASK;
  743. X        maxmaxcode = 1 << maxbits;
  744. X        fsize = 100000;     /* assume stdin large for USERMEM */
  745. X        if(maxbits > BITS) {
  746. X            fprintf(stderr,
  747. X            "stdin: compressed with %d bits, can only handle %d bits\n",
  748. X            maxbits, BITS);
  749. X            exit(1);
  750. X        }
  751. X        }
  752. X#ifndef DEBUG
  753. X        decompress();
  754. X#else
  755. X        if (debug == 0)     decompress();
  756. X        else        printcodes();
  757. X        if (verbose)        dump_tab();
  758. X#endif /* DEBUG */
  759. X    }
  760. X    }
  761. X    exit(exit_stat);
  762. X}
  763. X
  764. Xstatic int offset;
  765. Xlong int in_count = 1;            /* length of input */
  766. Xlong int bytes_out;            /* length of compressed output */
  767. Xlong int out_count = 0;         /* # of codes output (for debugging) */
  768. X
  769. X/*
  770. X * compress stdin to stdout
  771. X *
  772. X * Algorithm:  use open addressing double hashing (no chaining) on the
  773. X * prefix code / next character combination.  We do a variant of Knuth's
  774. X * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
  775. X * secondary probe.  Here, the modular division first probe is gives way
  776. X * to a faster exclusive-or manipulation.  Also do block compression with
  777. X * an adaptive reset, whereby the code table is cleared when the compression
  778. X * ratio decreases, but after the table fills.    The variable-length output
  779. X * codes are re-sized at this point, and a special CLEAR code is generated
  780. X * for the decompressor.  Late addition:  construct the table according to
  781. X * file size for noticeable speed improvement on small files.  Please direct
  782. X * questions about this implementation to ames!jaw.
  783. X */
  784. X
  785. Xstatic void compress() {
  786. X    register long fcode;
  787. X    register code_int i = 0;
  788. X    register int c;
  789. X    register code_int ent;
  790. X#ifdef XENIX_16
  791. X    register code_int disp;
  792. X#else    /* Normal machine */
  793. X    register int disp;
  794. X#endif
  795. X    register code_int hsize_reg;
  796. X    register int hshift;
  797. X
  798. X#ifndef COMPATIBLE
  799. X    if (nomagic == 0) {
  800. X    putchar(magic_header[0]); putchar(magic_header[1]);
  801. X    putchar((char)(maxbits | block_compress));
  802. X    if(ferror(stdout))
  803. X        writeerr();
  804. X    }
  805. X#endif /* COMPATIBLE */
  806. X
  807. X    offset = 0;
  808. X    bytes_out = 3;        /* includes 3-byte header mojo */
  809. X    out_count = 0;
  810. X    clear_flg = 0;
  811. X    ratio = 0;
  812. X    in_count = 1;
  813. X    checkpoint = CHECK_GAP;
  814. X    maxcode = MAXCODE(n_bits = INIT_BITS);
  815. X    free_ent = ((block_compress) ? FIRST : 256 );
  816. X
  817. X    ent = getchar ();
  818. X
  819. X    hshift = 0;
  820. X    for ( fcode = (long) hsize;  fcode < 65536L; fcode *= 2L )
  821. X    hshift++;
  822. X    hshift = 8 - hshift;        /* set hash code range bound */
  823. X
  824. X    hsize_reg = hsize;
  825. X    cl_hash( (count_int) hsize_reg);            /* clear hash table */
  826. X
  827. X#ifdef SIGNED_COMPARE_SLOW
  828. X    while ( (c = getchar()) != (unsigned) EOF ) {
  829. X#else
  830. X    while ( (c = getchar()) != EOF ) {
  831. X#endif
  832. X    in_count++;
  833. X    fcode = (long) (((long) c << maxbits) + ent);
  834. X    i = ((c << hshift) ^ ent);      /* xor hashing */
  835. X
  836. X    if ( htabof (i) == fcode ) {
  837. X        ent = codetabof (i);
  838. X        continue;
  839. X    } else if ( (long)htabof (i) < 0 )      /* empty slot */
  840. X        goto nomatch;
  841. X    disp = hsize_reg - i;        /* secondary hash (after G. Knott) */
  842. X    if ( i == 0 )
  843. X        disp = 1;
  844. Xprobe:
  845. X    if ( (i -= disp) < 0 )
  846. X        i += hsize_reg;
  847. X
  848. X    if ( htabof (i) == fcode ) {
  849. X        ent = codetabof (i);
  850. X        continue;
  851. X    }
  852. X    if ( (long)htabof (i) > 0 )
  853. X        goto probe;
  854. Xnomatch:
  855. X    output ( (code_int) ent );
  856. X    out_count++;
  857. X    ent = c;
  858. X#ifdef SIGNED_COMPARE_SLOW
  859. X    if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
  860. X#else
  861. X    if ( free_ent < maxmaxcode ) {
  862. X#endif
  863. X        codetabof (i) = free_ent++; /* code -> hashtable */
  864. X        htabof (i) = fcode;
  865. X    }
  866. X    else if ( (count_int)in_count >= checkpoint && block_compress )
  867. X        cl_block ();
  868. X    }
  869. X    /*
  870. X     * Put out the final code.
  871. X     */
  872. X    output( (code_int)ent );
  873. X    out_count++;
  874. X    output( (code_int)-1 );
  875. X
  876. X    /*
  877. X     * Print out stats on stderr
  878. X     */
  879. X    if(zcat_flg == 0 && !quiet) {
  880. X#ifdef DEBUG
  881. X    fprintf( stderr,
  882. X        "%ld chars in, %ld codes (%ld bytes) out, compression factor: ",
  883. X        in_count, out_count, bytes_out );
  884. X    prratio( stderr, in_count, bytes_out );
  885. X    fprintf( stderr, "\n");
  886. X    fprintf( stderr, "\tCompression as in compact: " );
  887. X    prratio( stderr, in_count-bytes_out, in_count );
  888. X    fprintf( stderr, "\n");
  889. X    fprintf( stderr, "\tLargest code (of last block) was %d (%d bits)\n",
  890. X        free_ent - 1, n_bits );
  891. X#else /* !DEBUG */
  892. X    fprintf( stderr, "Compression: " );
  893. X    prratio( stderr, in_count-bytes_out, in_count );
  894. X#endif /* DEBUG */
  895. X    }
  896. X    if(bytes_out > in_count)    /* exit(2) if no savings */
  897. X    exit_stat = 2;
  898. X    return;
  899. X}
  900. X
  901. X/*****************************************************************
  902. X * TAG( output )
  903. X *
  904. X * Output the given code.
  905. X * Inputs:
  906. X *    code:    A n_bits-bit integer.  If == -1, then EOF.  This assumes
  907. X *        that n_bits =< (long)wordsize - 1.
  908. X * Outputs:
  909. X *    Outputs code to the file.
  910. X * Assumptions:
  911. X *    Chars are 8 bits long.
  912. X * Algorithm:
  913. X *    Maintain a BITS character long buffer (so that 8 codes will
  914. X * fit in it exactly).    Use the VAX insv instruction to insert each
  915. X * code in turn.  When the buffer fills up empty it and start over.
  916. X */
  917. X
  918. Xstatic char buf[BITS];
  919. X
  920. X#ifndef vax
  921. Xchar_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
  922. Xchar_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
  923. X#endif /* vax */
  924. X
  925. Xvoid
  926. Xoutput( code )
  927. Xcode_int  code;
  928. X{
  929. X#ifdef DEBUG
  930. X    static int col = 0;
  931. X#endif /* DEBUG */
  932. X
  933. X    /*
  934. X     * On the VAX, it is important to have the register declarations
  935. X     * in exactly the order given, or the asm will break.
  936. X     */
  937. X    register int r_off = offset, bits= n_bits;
  938. X    register char * bp = buf;
  939. X
  940. X#ifdef DEBUG
  941. X    if ( verbose )
  942. X        fprintf( stderr, "%5d%c", code,
  943. X            (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
  944. X#endif /* DEBUG */
  945. X    if ( code >= 0 ) {
  946. X#ifdef vax
  947. X    /* VAX DEPENDENT!! Implementation on other machines is below.
  948. X     *
  949. X     * Translation: Insert BITS bits from the argument starting at
  950. X     * offset bits from the beginning of buf.
  951. X     */
  952. X    0;    /* Work around for pcc -O bug with asm and if stmt */
  953. X    asm( "insv      4(ap),r11,r10,(r9)" );
  954. X#else /* not a vax */
  955. X/*
  956. X * byte/bit numbering on the VAX is simulated by the following code
  957. X */
  958. X    /*
  959. X     * Get to the first byte.
  960. X     */
  961. X    bp += (r_off >> 3);
  962. X    r_off &= 7;
  963. X    /*
  964. X     * Since code is always >= 8 bits, only need to mask the first
  965. X     * hunk on the left.
  966. X     */
  967. X    *bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off];
  968. X    bp++;
  969. X    bits -= (8 - r_off);
  970. X    code >>= 8 - r_off;
  971. X    /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  972. X    if ( bits >= 8 ) {
  973. X        *bp++ = code;
  974. X        code >>= 8;
  975. X        bits -= 8;
  976. X    }
  977. X    /* Last bits. */
  978. X    if(bits)
  979. X        *bp = code;
  980. X#endif /* vax */
  981. X    offset += n_bits;
  982. X    if ( offset == (n_bits << 3) ) {
  983. X        bp = buf;
  984. X        bits = n_bits;
  985. X        bytes_out += bits;
  986. X        do
  987. X        putchar(*bp++);
  988. X        while(--bits);
  989. X        offset = 0;
  990. X    }
  991. X
  992. X    /*
  993. X     * If the next entry is going to be too big for the code size,
  994. X     * then increase it, if possible.
  995. X     */
  996. X    if ( free_ent > maxcode || (clear_flg > 0))
  997. X    {
  998. X        /*
  999. X         * Write the whole buffer, because the input side won't
  1000. X         * discover the size increase until after it has read it.
  1001. X         */
  1002. X        if ( offset > 0 ) {
  1003. X        if( fwrite( buf, 1, n_bits, stdout ) != n_bits)
  1004. X            writeerr();
  1005. X        bytes_out += n_bits;
  1006. X        }
  1007. X        offset = 0;
  1008. X
  1009. X        if ( clear_flg ) {
  1010. X        maxcode = MAXCODE (n_bits = INIT_BITS);
  1011. X        clear_flg = 0;
  1012. X        }
  1013. X        else {
  1014. X        n_bits++;
  1015. X        if ( n_bits == maxbits )
  1016. X            maxcode = maxmaxcode;
  1017. X        else
  1018. X            maxcode = MAXCODE(n_bits);
  1019. X        }
  1020. X#ifdef DEBUG
  1021. X        if ( debug ) {
  1022. X        fprintf( stderr, "\nChange to %d bits\n", n_bits );
  1023. X        col = 0;
  1024. X        }
  1025. X#endif /* DEBUG */
  1026. X    }
  1027. X    } else {
  1028. X    /*
  1029. X     * At EOF, write the rest of the buffer.
  1030. X     */
  1031. X    if ( offset > 0 )
  1032. X        fwrite( buf, 1, (offset + 7) / 8, stdout );
  1033. X    bytes_out += (offset + 7) / 8;
  1034. X    offset = 0;
  1035. X    fflush( stdout );
  1036. X#ifdef DEBUG
  1037. X    if ( verbose )
  1038. X        fprintf( stderr, "\n" );
  1039. X#endif /* DEBUG */
  1040. X    if( ferror( stdout ) )
  1041. X        writeerr();
  1042. X    }
  1043. X}
  1044. X
  1045. X/*
  1046. X * Decompress stdin to stdout.    This routine adapts to the codes in the
  1047. X * file building the "string" table on-the-fly; requiring no table to
  1048. X * be stored in the compressed file.  The tables used herein are shared
  1049. X * with those of the compress() routine.  See the definitions above.
  1050. X */
  1051. X
  1052. Xstatic void decompress() {
  1053. X    register char_type *stackp;
  1054. X    register int finchar;
  1055. X    register code_int code, oldcode, incode;
  1056. X
  1057. X    /*
  1058. X     * As above, initialize the first 256 entries in the table.
  1059. X     */
  1060. X    maxcode = MAXCODE(n_bits = INIT_BITS);
  1061. X    for ( code = 255; code >= 0; code-- ) {
  1062. X    tab_prefixof(code) = 0;
  1063. X    tab_suffixof(code) = (char_type)code;
  1064. X    }
  1065. X    free_ent = ((block_compress) ? FIRST : 256 );
  1066. X
  1067. X    finchar = oldcode = getcode();
  1068. X    if(oldcode == -1)   /* EOF already? */
  1069. X    return;         /* Get out of here */
  1070. X    putchar( (char)finchar );           /* first code must be 8 bits = char */
  1071. X    if(ferror(stdout))          /* Crash if can't write */
  1072. X    writeerr();
  1073. X    stackp = de_stack;
  1074. X
  1075. X    while ( (code = getcode()) > -1 ) {
  1076. X
  1077. X    if ( (code == CLEAR) && block_compress ) {
  1078. X        for ( code = 255; code >= 0; code-- )
  1079. X        tab_prefixof(code) = 0;
  1080. X        clear_flg = 1;
  1081. X        free_ent = FIRST - 1;
  1082. X        if ( (code = getcode ()) == -1 )    /* O, untimely death! */
  1083. X        break;
  1084. X    }
  1085. X    incode = code;
  1086. X    /*
  1087. X     * Special case for KwKwK string.
  1088. X     */
  1089. X    if ( code >= free_ent ) {
  1090. X        *stackp++ = finchar;
  1091. X        code = oldcode;
  1092. X    }
  1093. X
  1094. X    /*
  1095. X     * Generate output characters in reverse order
  1096. X     */
  1097. X#ifdef SIGNED_COMPARE_SLOW
  1098. X    while ( ((unsigned long)code) >= ((unsigned long)256) ) {
  1099. X#else
  1100. X    while ( code >= 256 ) {
  1101. X#endif
  1102. X        *stackp++ = tab_suffixof(code);
  1103. X        code = tab_prefixof(code);
  1104. X    }
  1105. X    *stackp++ = finchar = tab_suffixof(code);
  1106. X
  1107. X    /*
  1108. X     * And put them out in forward order
  1109. X     */
  1110. X    do
  1111. X        putchar ( *--stackp );
  1112. X    while ( stackp > de_stack );
  1113. X
  1114. X    /*
  1115. X     * Generate the new entry.
  1116. X     */
  1117. X    if ( (code=free_ent) < maxmaxcode ) {
  1118. X        tab_prefixof(code) = (unsigned short)oldcode;
  1119. X        tab_suffixof(code) = finchar;
  1120. X        free_ent = code+1;
  1121. X    }
  1122. X    /*
  1123. X     * Remember previous code.
  1124. X     */
  1125. X    oldcode = incode;
  1126. X    }
  1127. X    fflush( stdout );
  1128. X    if(ferror(stdout))
  1129. X    writeerr();
  1130. X}
  1131. X
  1132. X/*****************************************************************
  1133. X * TAG( getcode )
  1134. X *
  1135. X * Read one code from the standard input.  If EOF, return -1.
  1136. X * Inputs:
  1137. X *    stdin
  1138. X * Outputs:
  1139. X *    code or -1 is returned.
  1140. X */
  1141. X
  1142. Xcode_int
  1143. Xgetcode() {
  1144. X    /*
  1145. X     * On the VAX, it is important to have the register declarations
  1146. X     * in exactly the order given, or the asm will break.
  1147. X     */
  1148. X    register code_int code;
  1149. X    static int offset = 0, size = 0;
  1150. X    static char_type buf[BITS];
  1151. X    register int r_off, bits;
  1152. X    register char_type *bp = buf;
  1153. X
  1154. X    if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) {
  1155. X    /*
  1156. X     * If the next entry will be too big for the current code
  1157. X     * size, then we must increase the size.  This implies reading
  1158. X     * a new buffer full, too.
  1159. X     */
  1160. X    if ( free_ent > maxcode ) {
  1161. X        n_bits++;
  1162. X        if ( n_bits == maxbits )
  1163. X        maxcode = maxmaxcode;    /* won't get any bigger now */
  1164. X        else
  1165. X        maxcode = MAXCODE(n_bits);
  1166. X    }
  1167. X    if ( clear_flg > 0) {
  1168. X        maxcode = MAXCODE (n_bits = INIT_BITS);
  1169. X        clear_flg = 0;
  1170. X    }
  1171. X    size = fread( buf, 1, n_bits, stdin );
  1172. X    if ( size <= 0 )
  1173. X        return -1;            /* end of file */
  1174. X    offset = 0;
  1175. X    /* Round size down to integral number of codes */
  1176. X    size = (size << 3) - (n_bits - 1);
  1177. X    }
  1178. X    r_off = offset;
  1179. X    bits = n_bits;
  1180. X#ifdef vax
  1181. X    asm( "extzv   r10,r9,(r8),r11" );
  1182. X#else /* not a vax */
  1183. X    /*
  1184. X     * Get to the first byte.
  1185. X     */
  1186. X    bp += (r_off >> 3);
  1187. X    r_off &= 7;
  1188. X    /* Get first part (low order bits) */
  1189. X#ifdef NO_UCHAR
  1190. X    code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
  1191. X#else
  1192. X    code = (*bp++ >> r_off);
  1193. X#endif /* NO_UCHAR */
  1194. X    bits -= (8 - r_off);
  1195. X    r_off = 8 - r_off;        /* now, offset into code word */
  1196. X    /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  1197. X    if ( bits >= 8 ) {
  1198. X#ifdef NO_UCHAR
  1199. X        code |= (*bp++ & 0xff) << r_off;
  1200. X#else
  1201. X        code |= *bp++ << r_off;
  1202. X#endif /* NO_UCHAR */
  1203. X        r_off += 8;
  1204. X        bits -= 8;
  1205. X    }
  1206. X    /* high order bits. */
  1207. X    code |= (*bp & rmask[bits]) << r_off;
  1208. X#endif /* vax */
  1209. X    offset += n_bits;
  1210. X
  1211. X    return code;
  1212. X}
  1213. X
  1214. Xchar *
  1215. Xrindex(s, c)            /* For those who don't have it in libc.a */
  1216. Xregister char *s, c;
  1217. X{
  1218. X    char *p;
  1219. X    for (p = NULL; *s; s++)
  1220. X        if (*s == c)
  1221. X        p = s;
  1222. X    return(p);
  1223. X}
  1224. X
  1225. X#ifdef DEBUG
  1226. Xprintcodes()
  1227. X{
  1228. X    /*
  1229. X     * Just print out codes from input file.  For debugging.
  1230. X     */
  1231. X    code_int code;
  1232. X    int col = 0, bits;
  1233. X
  1234. X    bits = n_bits = INIT_BITS;
  1235. X    maxcode = MAXCODE(n_bits);
  1236. X    free_ent = ((block_compress) ? FIRST : 256 );
  1237. X    while ( ( code = getcode() ) >= 0 ) {
  1238. X    if ( (code == CLEAR) && block_compress ) {
  1239. X        free_ent = FIRST - 1;
  1240. X        clear_flg = 1;
  1241. X    }
  1242. X    else if ( free_ent < maxmaxcode )
  1243. X        free_ent++;
  1244. X    if ( bits != n_bits ) {
  1245. X        fprintf(stderr, "\nChange to %d bits\n", n_bits );
  1246. X        bits = n_bits;
  1247. X        col = 0;
  1248. X    }
  1249. X    fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
  1250. X    }
  1251. X    putc( '\n', stderr );
  1252. X    exit( 0 );
  1253. X}
  1254. X
  1255. Xcode_int sorttab[1<<BITS];    /* sorted pointers into htab */
  1256. X
  1257. Xdump_tab()      /* dump string table */
  1258. X{
  1259. X    register int i, first;
  1260. X    register ent;
  1261. X#define STACK_SIZE    15000
  1262. X    int stack_top = STACK_SIZE;
  1263. X    register c;
  1264. X
  1265. X    if(do_decomp == 0) {        /* compressing */
  1266. X    register int flag = 1;
  1267. X
  1268. X    for(i=0; i<hsize; i++) {        /* build sort pointers */
  1269. X        if((long)htabof(i) >= 0) {
  1270. X            sorttab[codetabof(i)] = i;
  1271. X        }
  1272. X    }
  1273. X    first = block_compress ? FIRST : 256;
  1274. X    for(i = first; i < free_ent; i++) {
  1275. X        fprintf(stderr, "%5d: \"", i);
  1276. X        de_stack[--stack_top] = '\n';
  1277. X        de_stack[--stack_top] = '"';
  1278. X        stack_top = in_stack((htabof(sorttab[i])>>maxbits)&0xff,
  1279. X                     stack_top);
  1280. X        for(ent=htabof(sorttab[i]) & ((1<<maxbits)-1);
  1281. X            ent > 256;
  1282. X            ent=htabof(sorttab[ent]) & ((1<<maxbits)-1)) {
  1283. X            stack_top = in_stack(htabof(sorttab[ent]) >> maxbits,
  1284. X                        stack_top);
  1285. X        }
  1286. X        stack_top = in_stack(ent, stack_top);
  1287. X        fwrite( &de_stack[stack_top], 1, STACK_SIZE-stack_top, stderr);
  1288. X        stack_top = STACK_SIZE;
  1289. X    }
  1290. X   } else if(!debug) {  /* decompressing */
  1291. X
  1292. X       for ( i = 0; i < free_ent; i++ ) {
  1293. X       ent = i;
  1294. X       c = tab_suffixof(ent);
  1295. X       if ( isascii(c) && isprint(c) )
  1296. X           fprintf( stderr, "%5d: %5d/'%c'  \"",
  1297. X               ent, tab_prefixof(ent), c );
  1298. X       else
  1299. X           fprintf( stderr, "%5d: %5d/\\%03o \"",
  1300. X               ent, tab_prefixof(ent), c );
  1301. X       de_stack[--stack_top] = '\n';
  1302. X       de_stack[--stack_top] = '"';
  1303. X       for ( ; ent != NULL;
  1304. X           ent = (ent >= FIRST ? tab_prefixof(ent) : NULL) ) {
  1305. X           stack_top = in_stack(tab_suffixof(ent), stack_top);
  1306. X       }
  1307. X       fwrite( &de_stack[stack_top], 1, STACK_SIZE - stack_top, stderr );
  1308. X       stack_top = STACK_SIZE;
  1309. X       }
  1310. X    }
  1311. X}
  1312. X
  1313. Xint
  1314. Xin_stack(c, stack_top)
  1315. X    register c, stack_top;
  1316. X{
  1317. X    if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) {
  1318. X        de_stack[--stack_top] = c;
  1319. X    } else {
  1320. X        switch( c ) {
  1321. X        case '\n': de_stack[--stack_top] = 'n'; break;
  1322. X        case '\t': de_stack[--stack_top] = 't'; break;
  1323. X        case '\b': de_stack[--stack_top] = 'b'; break;
  1324. X        case '\f': de_stack[--stack_top] = 'f'; break;
  1325. X        case '\r': de_stack[--stack_top] = 'r'; break;
  1326. X        case '\\': de_stack[--stack_top] = '\\'; break;
  1327. X        default:
  1328. X        de_stack[--stack_top] = '0' + c % 8;
  1329. X        de_stack[--stack_top] = '0' + (c / 8) % 8;
  1330. X        de_stack[--stack_top] = '0' + c / 64;
  1331. X        break;
  1332. X        }
  1333. X        de_stack[--stack_top] = '\\';
  1334. X    }
  1335. X    return stack_top;
  1336. X}
  1337. X#endif /* DEBUG */
  1338. X
  1339. Xvoid
  1340. Xwriteerr()
  1341. X{
  1342. X    perror ( ofname );
  1343. X    unlink ( ofname );
  1344. X    exit ( 1 );
  1345. X}
  1346. X
  1347. Xvoid
  1348. Xcopystat(ifname, ofname)
  1349. Xchar *ifname, *ofname;
  1350. X{
  1351. X#ifdef unix
  1352. X    struct stat statbuf;
  1353. X    int mode;
  1354. X    time_t timep[2];
  1355. X
  1356. X    fclose(stdout);
  1357. X    if (stat(ifname, &statbuf)) {               /* Get stat on input file */
  1358. X    perror(ifname);
  1359. X    return;
  1360. X    }
  1361. X    if ((statbuf.st_mode & S_IFMT/*0170000*/) != S_IFREG/*0100000*/) {
  1362. X    if(quiet)
  1363. X        fprintf(stderr, "%s: ", ifname);
  1364. X    fprintf(stderr, " -- not a regular file: unchanged");
  1365. X    exit_stat = 1;
  1366. X    } else if (statbuf.st_nlink > 1) {
  1367. X    if(quiet)
  1368. X        fprintf(stderr, "%s: ", ifname);
  1369. X    fprintf(stderr, " -- has %d other links: unchanged",
  1370. X        statbuf.st_nlink - 1);
  1371. X    exit_stat = 1;
  1372. X    } else if (exit_stat == 2 && (!force)) { /* No compression: remove file.Z */
  1373. X    if(!quiet)
  1374. X        fprintf(stderr, " -- file unchanged");
  1375. X    } else {            /* ***** Successful Compression ***** */
  1376. X    exit_stat = 0;
  1377. X    mode = statbuf.st_mode & 07777;
  1378. X    if (chmod(ofname, mode))                /* Copy modes */
  1379. X        perror(ofname);
  1380. X    chown(ofname, statbuf.st_uid, statbuf.st_gid);  /* Copy ownership */
  1381. X    timep[0] = statbuf.st_atime;
  1382. X    timep[1] = statbuf.st_mtime;
  1383. X    utime(ofname, timep);   /* Update last accessed and modified times */
  1384. X    if (unlink(ifname))     /* Remove input file */
  1385. X        perror(ifname);
  1386. X    if(!quiet)
  1387. X        fprintf(stderr, " -- replaced with %s", ofname);
  1388. X    return;     /* Successful return */
  1389. X    }
  1390. X
  1391. X    /* Unsuccessful return -- one of the tests failed */
  1392. X    if (unlink(ofname))
  1393. X    perror(ofname);
  1394. X#endif
  1395. X}
  1396. X/*
  1397. X * This routine returns 1 if we are running in the foreground and stderr
  1398. X * is a tty.
  1399. X */
  1400. X#ifdef unix
  1401. Xforeground()
  1402. X{
  1403. X    if(bgnd_flag) { /* background? */
  1404. X        return(0);
  1405. X    } else {            /* foreground */
  1406. X        if(isatty(2)) {         /* and stderr is a tty */
  1407. X            return(1);
  1408. X        } else {
  1409. X            return(0);
  1410. X        }
  1411. X    }
  1412. X}
  1413. X#endif
  1414. X
  1415. Xvoid
  1416. Xonintr ( )
  1417. X{
  1418. X    unlink ( ofname );
  1419. X    exit ( 1 );
  1420. X}
  1421. X
  1422. Xvoid
  1423. Xoops ( )        /* wild pointer -- assume bad input */
  1424. X{
  1425. X    if ( do_decomp == 1 )
  1426. X    fprintf ( stderr, "uncompress: corrupt input\n" );
  1427. X    unlink ( ofname );
  1428. X    exit ( 1 );
  1429. X}
  1430. X
  1431. Xvoid
  1432. Xcl_block ()             /* table clear for block compress */
  1433. X{
  1434. X    register long int rat;
  1435. X
  1436. X    checkpoint = in_count + CHECK_GAP;
  1437. X#ifdef DEBUG
  1438. X    if ( debug ) {
  1439. X        fprintf ( stderr, "count: %ld, ratio: ", in_count );
  1440. X        prratio ( stderr, in_count, bytes_out );
  1441. X        fprintf ( stderr, "\n");
  1442. X    }
  1443. X#endif /* DEBUG */
  1444. X
  1445. X    if(in_count > 0x007fffff) { /* shift will overflow */
  1446. X    rat = bytes_out >> 8;
  1447. X    if(rat == 0) {          /* Don't divide by zero */
  1448. X        rat = 0x7fffffff;
  1449. X    } else {
  1450. X        rat = in_count / rat;
  1451. X    }
  1452. X    } else {
  1453. X    rat = (in_count << 8) / bytes_out;      /* 8 fractional bits */
  1454. X    }
  1455. X    if ( rat > ratio ) {
  1456. X    ratio = rat;
  1457. X    } else {
  1458. X    ratio = 0;
  1459. X#ifdef DEBUG
  1460. X    if(verbose)
  1461. X        dump_tab();     /* dump string table */
  1462. X#endif
  1463. X    cl_hash ( (count_int) hsize );
  1464. X    free_ent = FIRST;
  1465. X    clear_flg = 1;
  1466. X    output ( (code_int) CLEAR );
  1467. X#ifdef DEBUG
  1468. X    if(debug)
  1469. X        fprintf ( stderr, "clear\n" );
  1470. X#endif /* DEBUG */
  1471. X    }
  1472. X}
  1473. X
  1474. Xvoid
  1475. Xcl_hash(hsize)          /* reset code table */
  1476. X    register count_int hsize;
  1477. X{
  1478. X#ifndef XENIX_16    /* Normal machine */
  1479. X    register count_int *htab_p = htab+hsize;
  1480. X#else
  1481. X    register j;
  1482. X    register long k = hsize;
  1483. X    register count_int *htab_p;
  1484. X#endif
  1485. X    register long i;
  1486. X    register long m1 = -1;
  1487. X
  1488. X#ifdef XENIX_16
  1489. X    for(j=0; j<=8 && k>=0; j++,k-=8192) {
  1490. X    i = 8192;
  1491. X    if(k < 8192) {
  1492. X        i = k;
  1493. X    }
  1494. X    htab_p = &(htab[j][i]);
  1495. X    i -= 16;
  1496. X    if(i > 0) {
  1497. X#else
  1498. X    i = hsize - 16;
  1499. X#endif
  1500. X    do {                /* might use Sys V memset(3) here */
  1501. X        *(htab_p-16) = m1;
  1502. X        *(htab_p-15) = m1;
  1503. X        *(htab_p-14) = m1;
  1504. X        *(htab_p-13) = m1;
  1505. X        *(htab_p-12) = m1;
  1506. X        *(htab_p-11) = m1;
  1507. X        *(htab_p-10) = m1;
  1508. X        *(htab_p-9) = m1;
  1509. X        *(htab_p-8) = m1;
  1510. X        *(htab_p-7) = m1;
  1511. X        *(htab_p-6) = m1;
  1512. X        *(htab_p-5) = m1;
  1513. X        *(htab_p-4) = m1;
  1514. X        *(htab_p-3) = m1;
  1515. X        *(htab_p-2) = m1;
  1516. X        *(htab_p-1) = m1;
  1517. X        htab_p -= 16;
  1518. X    } while ((i -= 16) >= 0);
  1519. X#ifdef XENIX_16
  1520. X    }
  1521. X    }
  1522. X#endif
  1523. X    for ( i += 16; i > 0; i-- )
  1524. X        *--htab_p = m1;
  1525. X}
  1526. X
  1527. Xvoid
  1528. Xprratio(stream, num, den)
  1529. XFILE *stream;
  1530. Xlong int num, den;
  1531. X{
  1532. X    register int q;         /* Doesn't need to be long */
  1533. X
  1534. X    if(num > 214748L) {             /* 2147483647/10000 */
  1535. X        q = num / (den / 10000L);
  1536. X    } else {
  1537. X        q = 10000L * num / den;     /* Long calculations, though */
  1538. X    }
  1539. X    if (q < 0) {
  1540. X        putc('-', stream);
  1541. X        q = -q;
  1542. X    }
  1543. X    fprintf(stream, "%d.%02d%%", q / 100, q % 100);
  1544. X}
  1545. X
  1546. Xvoid
  1547. Xversion()
  1548. X{
  1549. X    fprintf(stderr, "%s\n", rcs_ident);
  1550. X    fprintf(stderr, "Options: ");
  1551. X#ifdef vax
  1552. X    fprintf(stderr, "vax, ");
  1553. X#endif
  1554. X#ifdef NO_UCHAR
  1555. X    fprintf(stderr, "NO_UCHAR, ");
  1556. X#endif
  1557. X#ifdef SIGNED_COMPARE_SLOW
  1558. X    fprintf(stderr, "SIGNED_COMPARE_SLOW, ");
  1559. X#endif
  1560. X#ifdef XENIX_16
  1561. X    fprintf(stderr, "XENIX_16, ");
  1562. X#endif
  1563. X#ifdef COMPATIBLE
  1564. X    fprintf(stderr, "COMPATIBLE, ");
  1565. X#endif
  1566. X#ifdef DEBUG
  1567. X    fprintf(stderr, "DEBUG, ");
  1568. X#endif
  1569. X#ifdef BSD4_2
  1570. X    fprintf(stderr, "BSD4_2, ");
  1571. X#endif
  1572. X    fprintf(stderr, "BITS = %d\n", BITS);
  1573. X}
  1574. X
  1575. X/*
  1576. X *    Get the size of the file.  Although this is also possible on
  1577. X *    the Amiga, I have not yet implemented this as it is apparently
  1578. X *    used only to adapt the algorithm to make it more efficient for
  1579. X *    small files.  For files on the standard input, the file size
  1580. X *    is unknown, and 100000 is assumed, so we use that value here.
  1581. X *    Fred Fish  14-Jan-86
  1582. X */
  1583. X
  1584. Xlong
  1585. Xgetfilesize (name)
  1586. Xchar *name;
  1587. X{
  1588. X#ifdef unix
  1589. X    struct stat statbuf;
  1590. X
  1591. X    stat ( *fileptr, &statbuf );
  1592. X    return (statbuf.st_size);
  1593. X#else
  1594. X    return (100000);
  1595. X#endif
  1596. X}
  1597. X
  1598. X#ifndef unix
  1599. X#ifndef AMIGA
  1600. Xvoid
  1601. Xperror (arg)
  1602. Xchar *arg;
  1603. X{
  1604. X    if (arg && *arg) {
  1605. X        fprintf (stderr, "%s: ", arg);
  1606. X    }
  1607. X    fprintf (stderr, "<unknown error>\n");
  1608. X}
  1609. X#endif
  1610. X#endif
  1611. END_OF_FILE
  1612. if test 41167 -ne `wc -c <'uucp2/src/unix/compress.c'`; then
  1613.     echo shar: \"'uucp2/src/unix/compress.c'\" unpacked with wrong size!
  1614. fi
  1615. # end of 'uucp2/src/unix/compress.c'
  1616. fi
  1617. echo shar: End of archive 12 \(of 12\).
  1618. cp /dev/null ark12isdone
  1619. MISSING=""
  1620. for I in 1 2 3 4 5 6 7 8 9 10 11 12 ; do
  1621.     if test ! -f ark${I}isdone ; then
  1622.     MISSING="${MISSING} ${I}"
  1623.     fi
  1624. done
  1625. if test "${MISSING}" = "" ; then
  1626.     echo You have unpacked all 12 archives.
  1627.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1628. else
  1629.     echo You still need to unpack the following archives:
  1630.     echo "        " ${MISSING}
  1631. fi
  1632. ##  End of shell archive.
  1633. exit 0
  1634. -- 
  1635. Mail submissions (sources or binaries) to <amiga@cs.odu.edu>.
  1636. Mail comments to the moderator at <amiga-request@cs.odu.edu>.
  1637. Post requests for sources, and general discussion to comp.sys.amiga.
  1638.